package com.glory.dgcpcpda.activity.mfg;

/**
 * 批次下架功能
 */

import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

public class LotDismountActivity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.tvCircuit)
    TextView tvCircuit;
    @BindView(R.id.tvUserOrder)
    TextView tvUserOrder;
    @BindView(R.id.tvProductionOrder)
    TextView tvProductionOrder;
    @BindView(R.id.etShelves)
    EditText etShelves;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_lot_dismount;
    }

    @Override
    protected void initView() {
        tvTitle.setText("批次下架");
        setMainLayout(llMain);

        initEdit(etLot, s -> {
            //获取lot信息
            LotUtils.searchLotInfo(LotDismountActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
                @Override
                public void onSuccess(LotInfoEntity response) {
                    tvUserOrder.setText(response.getLOT().getCUSTOMERORDER());
                    tvProductionOrder.setText(response.getLOT().getWOID());
                    tvCircuit.setText(response.getPRODUCTMODEL());
                }

                @Override
                public void onFail(Throwable t) {
                    loadError(t, "LOTQUERY");
                }
            });
        });
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.tvBaseSubmit)
    public void onViewClicked() {
        if (actionControl("Submit11")) {
            return;
        }

        if (getText(etLot).equals("") || getText(etShelves).equals("")){
            showToast("批次号或货架号不能为空",false);
            return;
        }

        showSubmitDialog(new onSubmitListener() {
            @Override
            public void setOnSubmitListener() {
                FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JSONObject>>
                        (new TypeToken<BaseResponseBean<JSONObject>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                            showToast("下架成功", true);
                            LotDismountActivity.this.finish();
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<JSONObject>> response) {
                        super.onError(response);
                        loadError(response.getException(),"CPCMOVELOCATOR");
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE", "UNBIND");
                map.addProperty("LOTID", getText(etLot));
                map.addProperty("LOCATORID", getText(etShelves));
                NetUtils.requestPostNet("CPC.CPCMOVELOCATOR", "CPCMOVELOCATOR", this, map, callBack);
            }
        });
    }
}
